package com.et114.components.ectable;

import java.util.Collection;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.core.TableModel;
//import org.extremecomponents.table.view.html.CellBuilder;
import org.extremecomponents.util.HtmlBuilder;
import org.springframework.util.Assert;

/*
 * @author guanhw
 */
abstract public class MultipleSelectedCell implements Cell {
	protected  Log logger = LogFactory.getLog( this.getClass ( ) );
	public final static String	SELECTED_PRESIDENTS	= "SELECTED_PRESIDENTS";
	public final static String	SELECTED			= "SELECTED";
	public final static String	UNSELECTED			= "UNSELECTED";

	abstract public String getDataColName ( );
	abstract public String getShowColName ( );

	public String getExportDisplay ( TableModel model , Column column ) {
		return null;
	}
	

	public String getHtmlDisplay ( TableModel model , Column column ) {
		HtmlBuilder html = new HtmlBuilder();
		html.td ( 0 ).xclose ( ) ;
		try {
			Object bean = model.getCurrentRowBean();
			String dcn = getDataColName();
			String scn = getShowColName();
			Assert.notNull( dcn );
			Assert.notNull( scn );
			String id = BeanUtils.getProperty( bean, dcn );
			Collection selectedPresidentsIds = ( Collection ) model.getContext().getSessionAttribute( SELECTED_PRESIDENTS );
			if ( selectedPresidentsIds != null	&& selectedPresidentsIds.contains( id ) ) {
				html.input( "checkbox" ).name( scn ).value( BeanUtils.getProperty( bean, dcn ) );
				html.checked();
				html.xclose();
			} else {
				html.input( "checkbox" ).name( scn ).value(	BeanUtils.getProperty( bean, dcn ) );
				html.xclose();
			}
		} catch ( Exception e ) {
			logger.error (  e.getMessage ( ) , e ) ;
		}
		html.tdEnd ( );
		return html.toString();
	}
}
